//
//  RHBatteryView.m
//  RuiHeAppApp
//
//  Created by Yan Chen on 2024/1/5.
//

#import "RHBatteryView.h"
@interface RHBatteryView()
@property (nonatomic ,strong) CAGradientLayer *gradientLayer;

@property (nonatomic ,strong) UIImageView *batImageView;
@end
@implementation RHBatteryView

-(instancetype)init{
    if(self = [super init]){
        [self setupUI];
    }
    return self;
}

-(void)layoutSubviews
{
    _gradientLayer.frame = CGRectMake(3, 3, (self.width-8)*self.soc,self.height-6);
    if(self.isVertical){
        CGFloat h = (self.height - 17);
        _gradientLayer.frame = CGRectMake(7, 10 + h*(1-self.soc), (self.width - 14), h*self.soc);
    }
}

-(void)setIsVertical:(BOOL)isVertical
{
    _isVertical = isVertical;
    self.batImageView.image = [UIImage imageNamed:@"device_battery2"];
    [self updateSoc];

}
-(void)setSoc:(CGFloat)soc
{
    _soc = soc;
    [self updateSoc];
}

-(void)updateSoc
{
//    if(self.isVertical){
//        _gradientLayer.frame = CGRectMake(7, 10, (self.width - 14), (self.height - 17)*self.soc);
//    }
    
    [self setNeedsLayout];
    [self layoutIfNeeded];
}

-(void)setupUI
{
    _gradientLayer = [[CAGradientLayer alloc]init];
    [self.layer addSublayer:_gradientLayer];
    _gradientLayer.frame = CGRectMake(0, 0, 32, 18);
    _gradientLayer.startPoint = CGPointMake(0, 0.5);
    _gradientLayer.endPoint = CGPointMake(1, 0.5);
    _gradientLayer.colors = @[(id)kCOLOR_HEX(@"6FE452").CGColor,(id)kCOLOR_HEX(@"AFF485").CGColor];
    _gradientLayer.locations = @[@(0),@(1)];
    
    UIImageView *batImageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"device_battery1"]];
    [self addSubview:batImageView];
    self.batImageView = batImageView;
    [batImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.insets(UIEdgeInsetsZero);
    }];
    
}

@end
